package com.quanquan.web.task;

import com.alibaba.fastjson.JSONObject;
import com.quanquan.entity.StudentInfo;
import com.quanquan.service.StudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class QuartzService {

    @Autowired
    private StudentInfoService studentInfoService;
    //    每分钟启动
    @Scheduled(cron = "0 0/1 * * * ?")
    public void timerToNow(){
        System.out.println("-----------------------定时器开启----------------------");
        List<StudentInfo> studentInfoList = studentInfoService.findByOnline("online");
        System.out.println(JSONObject.toJSONString(studentInfoList.size()));
        if (studentInfoList != null && studentInfoList.size() > 0) {
            for (StudentInfo student : studentInfoList) {
                if (student.getLastTime() == null || System.currentTimeMillis() - student.getLastTime() > 60*1000L) {
                    student.setOnline("offline");
                    studentInfoService.save(student);
                }
            }
        }
    }

}
